package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dm.jdbc.util.StringUtil;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.*;
import io.renren.dto.TradermDTO;
import io.renren.dto.UserInfoDto;
import io.renren.entity.*;
import io.renren.service.TradermService;
import cn.hutool.core.util.StrUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-21
 */
@Service
public class TradermServiceImpl extends CrudServiceImpl<TradermDao, TradermEntity, TradermDTO> implements TradermService {

    private final AutoDao autoDao;
    @Autowired
    private AutobrandDao autobrandDao;
    @Autowired
    private AutogroupDao autogroupDao;
    @Autowired
    private AutomodelsDao automodelsDao;

    public TradermServiceImpl(AutoDao autoDao) {
        super();
        this.autoDao = autoDao;
    }

    @Override
    public QueryWrapper<TradermEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<TradermEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public TradermEntity findOneByTel(String mobile) {
        QueryWrapper<TradermEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("tel", mobile);
        wrapper.apply("(StopDate is Null or StopDate = '1900-01-01' or StopDate > getdate())");
        return baseDao.selectOne(wrapper);
    }

    @Override
    public TradermEntity findOneByTraderMOID(String tradermFk) {
        QueryWrapper<TradermEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("TraderMOID", tradermFk);
        return baseDao.selectOne(wrapper);
    }

    @Override
    public Map<String, Object> memberInfo(String traderMOID) {
        Map<String, Object> result = new HashMap<>();
        TradermEntity tradermEntity = findOneByTraderMOID(traderMOID);
        result.put("TraderMOID", tradermEntity);
        result.put("userId", tradermEntity.getTel());
        result.put("Name", tradermEntity.getName());
        result.put("headImg", tradermEntity.getHeadpicurl());
        result.put("hasNewMsg", 1);
        result.put("MiniOpenID", tradermEntity.getMiniopenid());
        result.put("WxOpenID", tradermEntity.getWxopenid());
        // 查询卡券数量

        Integer countDis = baseDao.getDiscountInfo(traderMOID);
        result.put("couponsNum", countDis);

        List<Map<String, Object>> list = autoDao.selectMaps(new QueryWrapper<AutoEntity>()
                .select("AutoOID,AutoNo,AutoBrand_FK,AutoGroup_FK,AutoModels_FK,IsDefault,InternalVehicleSign,CompulsoryInsuranceEndDate,YearlyCheckDate,CommercialInsuranceEndDate,ServiceMaintenanceDate")
                .eq("TraderM_FK", traderMOID).orderByDesc("IsDefault"));
        for (Map<String, Object> item : list) {
            item.put("AutoNo", item.get("AutoNo").toString());
            QueryWrapper<AutobrandEntity> wrapperA = new QueryWrapper<>();
            wrapperA.select("Name");
            wrapperA.eq("AutoBrandOID", item.get("AutoBrand_FK").toString());
            List<AutobrandEntity> autobrandEntities = autobrandDao.selectList(wrapperA);
            if (autobrandEntities.size() > 0) {
                item.put("AutoModelsName", autobrandEntities.get(0).getName());
            }

            QueryWrapper<AutogroupEntity> wrapperB = new QueryWrapper<>();
            wrapperB.select("Name");
            wrapperB.eq("AutoGroupOID", item.get("AutoGroup_FK").toString());
            List<AutogroupEntity> autogroupEntities = autogroupDao.selectList(wrapperB);
            if (autogroupEntities.size() > 0) {
                item.put("AutoModelsName", autogroupEntities.get(0).getName());
            }

            QueryWrapper<AutomodelsEntity> wrapperC = new QueryWrapper<>();
            wrapperC.select("Name,AutoYear");
            wrapperC.eq("AutoModelsOID", item.get("AutoModels_FK").toString());
            List<AutomodelsEntity> autobrandEntities1 = automodelsDao.selectList(wrapperC);
            if (autobrandEntities1.size() > 0) {
                item.put("AutoModelsName", autobrandEntities1.get(0).getAutoyear() + "款" + autobrandEntities1.get(0).getName());
            }
            List<String> autoArr = autoDao.exSql(item.get("AutoOID").toString());
            if (autoArr.size() > 0) {
                item.put("identifyName", autoArr.get(0));
            }
        }
        result.put("autoList", list);

        return result;
    }

    @Override
    public TradermEntity updateUserInfo(String traderMOID, UserInfoDto dto) {
        TradermEntity tradermEntity = findOneByTraderMOID(traderMOID);
        if (tradermEntity != null) {
            if (StringUtil.isNotEmpty(dto.getUserName())) {
                baseDao.updateUserNameInfo(dto.getUserName(), tradermEntity.getTradermoid());
            }
            if (StringUtil.isNotEmpty(dto.getHeadImg())) {
                baseDao.updateUserHeadInfo(dto.getHeadImg(), tradermEntity.getTradermoid());
            }
        }
        return tradermEntity;
    }

    @Override
    public TradermEntity findByMiniOpenId(String miniprogramOpenId) {
        List<TradermEntity> list = baseDao.selectList(new QueryWrapper<TradermEntity>()
                .eq("MiniOpenID", miniprogramOpenId));
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public TradermEntity findByWxOpenId(String openId) {
        List<TradermEntity> list = baseDao.selectList(new QueryWrapper<TradermEntity>()
                .eq("WXOpenID", openId));
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public void updateWxOpenIdByMiniOpenId(String wxOpenId, String miniOpenId) {
        TradermEntity tradermEntity = new TradermEntity();
        tradermEntity.setWxopenid(wxOpenId);
        baseDao.update(tradermEntity, new QueryWrapper<TradermEntity>().eq("MiniOpenID", miniOpenId));
    }

    @Override
    public List<TradermEntity> findListByTel(String mobile) {
        QueryWrapper<TradermEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("tel", mobile);
        wrapper.apply("(StopDate is Null or StopDate = '1900-01-01' or StopDate > getdate())");
        return baseDao.selectList(wrapper);
    }

    @Override
    public void setWxOpenIdIsNull(String tradermoid) {

        baseDao.setWxOpenIdIsNull(tradermoid);
    }

    private String HideAutoNo(String autoNo) {
        String[] arr = autoNo.split("-");
        String prefix = arr[0];
        String suffix = arr[1].substring(4, arr[1].length());
        return prefix + "****" + suffix;
    }
}